{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 3 4 5 ------------\n",
      "[1, 2, 3, 4, 5, 6]\n"
     ]
    }
   ],
   "source": [
    "list1=[1,2,3,4,5]\n",
    "for i in range(len(list1)):\n",
    "    print(list1[i],end=' ')\n",
    "print(\"------------\")\n",
    "list1.append(6)\n",
    "print(list1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b1a77358",
   "metadata": {},
   "outputs": [],
   "source": [
    "#isinstance()是否可迭代对象\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85968074",
   "metadata": {},
   "source": [
    "* 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6aa3a288",
   "metadata": {},
   "outputs": [],
   "source": [
    "def opo(a,b):\n",
    "    print(\"isone\")\n",
    "    return \n",
    "\n",
    "# -*- coding: UTF-8 -*-\n",
    "\n",
    "def sum(num1,num2):\n",
    "\t# 两数之和     类型检查\n",
    "\tif not (isinstance (num1,(int ,float)) and isinstance (num2,(int ,float))):\n",
    "\t\traise TypeError('参数类型错误')\n",
    "\treturn num1+num2\n",
    "\n",
    "print(sum(1,2))\n",
    "\n",
    "# -*- coding: UTF-8 -*-\n",
    "\n",
    "def  division ( num1, num2 ):\n",
    "\t# 求商与余数\n",
    "         a = num1 % num2\n",
    "         b = (num1-a) / num2\n",
    "         return b , a\n",
    "\n",
    "num1 , num2 = division(9,4)\n",
    "tuple1 = division(9,4)\n",
    "\n",
    "print (num1,num2)\n",
    "print (tuple1)\n",
    "\n",
    "#默认参数的值是不可变的对象，比如None、True、False、数字或字符串\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b6d6a183",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "两点水 23 男 "
     ]
    }
   ],
   "source": [
    "# 3、for 循环也可以迭代 dict （字典）\n",
    "dict1 = {'name':'两点水','age':'23','sex':'男'}\n",
    "\n",
    "for key in dict1 :    # 迭代 dict 中的 key\n",
    "    print ( dict1.get(key), end = ' ' )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c02d100e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "l i a n g d i a n s h u i \n",
      "------------------------\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "# 1、字符创创建迭代器对象\n",
    "str1 = 'liangdianshui'\n",
    "iter1 = iter ( str1 )\n",
    "\n",
    "# 2、list对象创建迭代器\n",
    "list1 = [1,2,3,4]\n",
    "iter2 = iter ( list1 )\n",
    "\n",
    "# 3、tuple(元祖) 对象创建迭代器\n",
    "tuple1 = ( 1,2,3,4 )\n",
    "iter3 = iter ( tuple1 )\n",
    "\n",
    "# for 循环遍历迭代器对象\n",
    "for x in iter1 :\n",
    "    print ( x , end = ' ' )\n",
    "\n",
    "print('\\n------------------------')\n",
    "# next() 函数遍历迭代器\n",
    "while True :\n",
    "    try :\n",
    "       print ( next ( iter3 ) )\n",
    "    except StopIteration :\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f6e2f362",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "[(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5)]\n"
     ]
    }
   ],
   "source": [
    "# 使用列表作为迭代器\n",
    "my_list = [1, 2, 3, 4, 5]\n",
    "iterator = iter(my_list)\n",
    "\n",
    "print(next(iterator))  # 输出: 1\n",
    "print(next(iterator))  # 输出: 2\n",
    "print(next(iterator))  # 输出: 3\n",
    "\n",
    "# -*- coding: UTF-8 -*-\n",
    "list1= [(x+1,y+1) for x in range(3) for y in range(5)] \n",
    "print(list1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
